<?php 
class class_sanpham extends database
{
		
	function ChiTietSanPham($MaSanPham)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag, s.Mota, s.HaveDacDiem 
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where s.MaSo='.$MaSanPham.' and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function ListDacDiemSanPham($MaSanPham)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select *
				from dac_diem_san_pham
				where MaSanPham='.$MaSanPham;
		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function TangView($MaSanPham)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'update san_pham
				set PageView=PageView+1
				where MaSo='.$MaSanPham;
		$this->setQuery($sql);
		$this->Query();
		
		$sql = 'select *
				from san_pham
				where MaSo='.$MaSanPham;
		$this->setQuery($sql);
		$row = $this->loadOneRow();
		
		$sql = 'update nguoi_ban
				set PageView=PageView+1
				where MaSo='.$row['MaNguoiBan'];
		$this->setQuery($sql);
		$this->Query();
	}
	
	function ListSanPhamViewNhieuNhat()
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0
				order by PageView desc limit 0,3';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function CountListSanPhamViewNhieuNhat()
	{
		$sql = 'select * 
				from san_pham
				where TinhTrangDang=1 and TinhTrangXoa=0';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		$num = count($table);
		return $num;
	}
	
	function ListSanPhamByMaNguoiBan($MaNguoiBan,$start,$limit)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where s.MaNguoiBan='.$MaNguoiBan.' and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0
				order by NgayDang desc limit '.$start.','.$limit;

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function CountListSanPhamByMaNguoiBan($MaNguoiBan)
	{
		$sql = 'select * 
				from san_pham
				where MaNguoiBan='.$MaNguoiBan.' and TinhTrangDang=1 and TinhTrangXoa=0';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		$num = count($table);
		return $num;
	}
	
	function ListTagByMaSanPham($MaSanPham)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select * 
				from chi_tiet_tag c, tag t
				where c.MaSanPham='.$MaSanPham.' and c.MaTag = t.MaSo';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	// Tìm kiếm cơ bản ///////////////////////////////////////////////////////////////////////////////////////////
	function ListSanPhamByTuKhoaCoBan($TuKhoa,$LoaiSort,$start,$limit)
	{
		$this->setQuery('set names utf8');
		$this->Query();
		
		if ($TuKhoa == "")
		{
			$sql = "select * from san_pham where 1=0";
			$this->setQuery($sql);
			$table = $this->LoadAllRow();
			return $table;
		}
		
		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where (s.TenSanPham like "%'.$TuKhoa.'%" and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0)
					or (s.NhaSanXuat like "%'.$TuKhoa.'%" and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0)
					or (s.Mota like "%'.$TuKhoa.'%" and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0)';
		switch ($LoaiSort)
		{
			case 1:
				$sql .= ' order by NgayDang desc ';
				break;
			case 2:
				$sql .= ' order by PageView desc ';
				break;
			case 3:
				$sql .= ' order by TenSanPham asc ';
				break;
		}
		
		$sql .= 'limit '.$start.','.$limit;

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function CountListSanPhamByTuKhoaCoBan($TuKhoa)
	{
		if ($TuKhoa == "")
		{
			return 0;
		}
		
		$sql = 'select * 
				from san_pham s
				where (s.TenSanPham like "%'.$TuKhoa.'%" and TinhTrangDang=1 and TinhTrangXoa=0)
					or (s.NhaSanXuat like "%'.$TuKhoa.'%" and TinhTrangDang=1 and TinhTrangXoa=0)
					or (s.Mota like "%'.$TuKhoa.'%" and TinhTrangDang=1 and TinhTrangXoa=0)';


		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		$num = count($table);
		return $num;
	}
	
	// Tìm kiếm nâng cao ///////////////////////////////////////////////////////////////////////////////////////////
	function ListSanPhamByTuKhoaNangCao($SanPham,$NguoiBan,$NhaSanXuat,$MoTa,$MaDanhMuc,$LoaiSort,$start,$limit)
	{
		$this->setQuery('set names utf8');
		$this->Query();
		
		
		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag, s.MoTa
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0';
		
		if ($MaDanhMuc <> 0)
		{
			$sql .= ' and s.MaDanhMuc='.$MaDanhMuc;
		}

		if ($SanPham <> "")
		{
			$sql .= ' and s.TenSanPham like "%'.$SanPham.'%"';
		}
		
		if ($NguoiBan <> "")
		{
			$sql .= ' and n.Username like "%'.$NguoiBan.'%"';
		}
		
		if ($NhaSanXuat <> "")
		{
			$sql .= ' and s.NhaSanXuat like "%'.$NhaSanXuat.'%"';
		}
		
		if ($MoTa <> "")
		{
			$sql .= ' and s.MoTa like "%'.$MoTa.'%"';
		}
		
		switch ($LoaiSort)
		{
			case 1:
				$sql .= ' order by NgayDang desc ';
				break;
			case 2:
				$sql .= ' order by PageView desc ';
				break;
			case 3:
				$sql .= ' order by TenSanPham asc ';
				break;
		}
		
		$sql .= 'limit '.$start.','.$limit;

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function CountListSanPhamByTuKhoaNangCao($SanPham,$NguoiBan,$NhaSanXuat,$MoTa,$MaDanhMuc)
	{
		
		$sql = 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag, s.MoTa
				from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv
				where s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo and s.TinhTrangDang=1 and s.TinhTrangXoa=0';
		
		if ($MaDanhMuc <> 0)
		{
			$sql .= ' and s.MaDanhMuc='.$MaDanhMuc;
		}

		if ($SanPham <> "")
		{
			$sql .= ' and s.TenSanPham like "%'.$SanPham.'%"';
		}
		
		if ($NguoiBan <> "")
		{
			$sql .= ' and n.Username like "%'.$NguoiBan.'%"';
		}
		
		if ($NhaSanXuat <> "")
		{
			$sql .= ' and s.NhaSanXuat like "%'.$NhaSanXuat.'%"';
		}
		
		if ($MoTa <> "")
		{
			$sql .= ' and s.MoTa like "%'.$MoTa.'%"';
		}

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		$num = count($table);
		return $num;
	}

}
?>